package com.hawthorn.redis.service.dict.impl;

import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.entity.dto.SysDictDTO;
import com.hawthorn.entity.po.SysDictPO;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.redis.repository.DictCacheMapper;
import com.hawthorn.redis.service.dict.DictCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @copyright: Copyright (c) 2021 andyten
 * <p></p>
 * @remark: todo 加载字典缓存服务实现类
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 3/4/21 11:05 PM
 * @lasted: 3/4/21 11:05 PM
 * @version v1.0.1
 */
@Service
@Slf4j
public class DictCacheServiceImpl implements DictCacheService
{
  @Autowired
  RedisMyClient redisMyClient;

  @Resource
  private DictCacheMapper dictCacheMapper;

  @Transactional(readOnly = true)
  public void init()
  {
    List<SysDictDTO> dictCache = dictCacheMapper.selectDict();
    String key;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DICT_PREFIX1, "*"));

    for (SysDictDTO dictCacheDTO : dictCache)
    {
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DICT_PREFIX, dictCacheDTO.getType(), dictCacheDTO.getDicCode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_CODE, dictCacheDTO.getDicCode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_LABEL, dictCacheDTO.getLabel());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_SPELL, dictCacheDTO.getSpell());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_ASPELL, dictCacheDTO.getAspell());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_PARENT, String.valueOf(dictCacheDTO.getParent()));
    }

    log.info("====== load dictdata to redis success ======");
  }

  @Override
  public void syncDictItem(SysDictPO sysDictPO)
  {
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DICT_PREFIX, sysDictPO.getType(), sysDictPO.getDicCode());
    // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_CODE, sysDictPO.getDicCode());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_LABEL, sysDictPO.getLabel());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_SPELL, sysDictPO.getSpell());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_ASPELL, sysDictPO.getAspell());
    // redisMyClient.hSet(key, RedisConstant.REDIS_KEY_DICT_PARENT, String.valueOf(sysDictPO.getParent()));

    log.info("====== reload dictdata to redis success ======");
  }

  @Override
  public void removeDictItem(String dicType, String dicCode)
  {
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DICT_PREFIX, dicType, dicCode);
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_DICT_PREFIX, "*"));

    log.info("====== remove dictdata to redis success ======");
  }
}
